package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.PageUtil;
import com.dongdongshop.pojo.StatusCode;
import com.dongdongshop.service.TbGoodsService;
import com.dongdongshop.service.TbItemService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goodsController2")
public class GoodsController {
    @Reference
    private TbItemService tbItemService;
    @Reference
    private TbGoodsService tbGoodsService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //跳转审核页面
    @RequestMapping("togoods")
    public String toGoods(){
        return "admin/goods";
    }

    //查询所有商品
    @RequestMapping("selectItem")
    @ResponseBody
    public StatusCode selectItem(String auditStatus, String goodsName, @RequestParam(defaultValue = "1")Integer pageNum,
                                 @RequestParam(defaultValue = "5")Integer pageSize){
        PageUtil page = tbItemService.findallItem(auditStatus,goodsName,pageNum,pageSize);
        return StatusCode.build(ConstantEnum.SUCCESS).setT(page);
    }

    //删除商品
    @RequestMapping("deleteGoods")
    @ResponseBody
    public StatusCode deleteGoods(Long[] ids){
        Boolean b = tbGoodsService.deleteGoods(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.ERROR);
    }

    //通过审核
    @RequestMapping("statusUpdateyes")
    @ResponseBody
    public StatusCode statusUpdateyes(Long[] ids){
        Boolean b = tbGoodsService.statusUpdateyes(ids);
        if (b){
            //如果审核通过 将id放入mq当中
            rocketMQTemplate.convertAndSend("id",ids);
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.ERROR);
    }

    //驳回
    @RequestMapping("statusUpdateno")
    @ResponseBody
    public StatusCode statusUpdateno(Long[] ids){
        Boolean b = tbGoodsService.statusUpdateno(ids);
        if (b){
            return StatusCode.build(ConstantEnum.UPDATE_SUCCESS);
        }
        return StatusCode.build(ConstantEnum.ERROR);
    }
}
